package com.yy.main.app.web;

import com.baomidou.mybatisplus.plugins.pagination.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yy.kit.annotation.HasPers;
import com.yy.kit.collections.YYMapKits;
import com.yy.kit.string.YYStrKits;
import com.yy.main.app.mapper.TBugMapper;
import com.yy.main.app.service.IPermissionService;
import com.yy.main.app.web.base.CommonController;
import com.yy.pojo.base.TBug;
import com.yy.pojo.base.TBugExample;
import com.yy.pojo.base.TUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * bug管理 前端控制器
 * </p>
 *
 * @author 杨阳
 * @since 2017年10月20日
 */
@Controller
@RequestMapping("/bug")
@Api(description = "bug管理前端控制器", tags = {"BUG管理controller"})
public class BugController extends CommonController {
    @Autowired
    private TBugMapper tBugMapper;
    @Resource(name = "iPermissionService")
    private IPermissionService iPermissionService;

    /**
     * <p>
     * 跳转到bug管理页面
     * </p>
     *
     * @return {@link ModelAndView}
     * @author 杨阳
     * @since 2017年9月4日16:51:27
     */
    @ApiOperation(value = "跳转到bug管理页面", httpMethod = "GET", notes = "跳转到bug管理页面")
    @RequestMapping(value = "/bugList")
    @HasPers(name = "bug:manager")
    public ModelAndView goBugPage(HttpServletRequest request) {
        TUser user = getCurrentUser(request.getSession());
        return goToPageByModelAndView("system/bug/bug_list", null, user.getId());
    }

    /**
     * <p>
     * 加载后台管理，bug管理的数据表格
     * </p>
     *
     * @param page   当前页数
     * @param limit  每页显示记录数
     * @param status bug状态
     * @param title  bug标题
     * @return {@link HashMap}
     * @author 杨阳
     * @since 2017年9月5日
     */
    @ApiOperation(value = "BUG数据表格", httpMethod = "GET", notes = "加载后台管理，bug管理的数据表格")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页数", required = true, paramType = "query"),
            @ApiImplicitParam(name = "limit", value = "每页显示记录数", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "bug状态", paramType = "query"),
            @ApiImplicitParam(name = "title", value = "bug标题", paramType = "query")
    })
    @RequestMapping(value = "/bugTable", method = RequestMethod.GET)
    public @ResponseBody
    Map<String, Object> bugTable(Integer page, Integer limit, String status, String title) {
        TBugExample example = new TBugExample();

        if (YYStrKits.isNotBlank(status)) {
            example.createCriteria().andStatusEqualTo(status);
        }

        if (YYStrKits.isNotBlank(title)) {
            example.createCriteria().andTitleLike(YYStrKits.like(title));
        }

        PageHelper.startPage(page, limit);
        List<TBug> bugList = tBugMapper.selectByExample(example);

        return super.pageToMap(new PageInfo(bugList));
    }

    /**
     * <p>
     * 跳转到编辑bug的页面
     * </p>
     *
     * @param id bugID
     * @return {@link ModelAndView}
     * @author 杨阳
     * @since 2017年9月6日
     */
    @ApiOperation(value = "跳转到编辑bug的页面", httpMethod = "GET", notes = "增加与修改为一个页面，用户必须包含bug:add,bug:update权限")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "bugID，如果是增加则为-1", required = true, paramType = "path")
    })
    @RequestMapping("/bugform/{id}")
    @HasPers(name = "bug:add,bug:update")
    public ModelAndView goBugForm(@PathVariable("id") String id, HttpServletRequest request) {
        TUser user = getCurrentUser(request.getSession());
        TBug bug = tBugMapper.selectByPrimaryKey(id);// 如果是新增id肯定是-1
        if (null == bug) {
            bug = new TBug();
        }
        Map<String, Object> param = YYMapKits.createHashMap();
        param.put("bug", bug);
        return goToPageByModelAndView("system/bug/bug_form", param, user.getId());
    }

    /**
     * <p>
     * 查看bug详情
     * </p>
     *
     * @return {@link ModelAndView}
     * @author 杨阳
     * @since 2017年9月11日
     */
    @ApiOperation(value = "查看bug详情", httpMethod = "GET", notes = "用户必须包含bug:show权限")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "bugID", required = true, paramType = "path")
    })
    @RequestMapping("/show/{id}")
    @HasPers(name = "bug:show")
    public ModelAndView show(@PathVariable("id") String id) {
        TBug bug = tBugMapper.selectByPrimaryKey(id);
        Map<String, Object> param = YYMapKits.createHashMap();
        param.put("bug", bug);
        return goToPageByModelAndView("system/bug/bug_show", param);
    }

    /**
     * <p>
     * 保存bug信息
     * </p>
     *
     * @param bug bug实体
     * @return {@link Map}
     * @author 杨阳
     * @since 2017年9月7日
     */
    @ApiOperation(value = "保存bug信息", httpMethod = "POST", notes = "用于修改或新增")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "bug", value = "bug实体类", required = true, paramType = "query")
    })
    @RequestMapping(value = "/savebug", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> savebug(TBug bug, HttpSession session) {
        TUser user = getCurrentUser(session);
        Map<String, Object> result = YYMapKits.createHashMap();
        if (YYStrKits.isBlank(bug.getId())) {// 增加
            bug.setId(YYStrKits.getTableId());
            bug.setCreate_date(new Date());
            bug.setCreater(user.getName());
            Integer b = tBugMapper.insert(bug);
            if (b > 0) {
                result.put("success", Boolean.TRUE);
                result.put("msg", "提交bug成功!!");
            } else {
                result.put("success", Boolean.FALSE);
                result.put("msg", "提交bug失败，请重试!!");
            }
        } else {// 修改
            bug.setUpdate_date(new Date());
            bug.setCreater(user.getName());
            Integer b = tBugMapper.updateByPrimaryKey(bug);
            if (b > 0) {
                result.put("success", Boolean.TRUE);
                result.put("msg", "修改bug成功!!");
            } else {
                result.put("success", Boolean.FALSE);
                result.put("msg", "修改bug失败，请重试!!");
            }
        }
        return result;
    }

    /**
     * <p>
     * 删除bug
     * </p>
     *
     * @param id bugID
     * @return {@link Map}
     * @author 杨阳
     * @since 2017年9月7日
     */
    @ApiOperation(value = "删除bug", httpMethod = "POST", notes = "删除bug，用户需要拥有bug:delete权限")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "bugID", required = true, paramType = "path")
    })
    @RequestMapping("/delbug/{id}")
    @HasPers(name = "bug:delete")
    @ResponseBody
    public Map<String, Object> delbug(@PathVariable("id") String id) {
        Map<String, Object> map = YYMapKits.createHashMap();
        Integer b = tBugMapper.deleteByPrimaryKey(id);
        if (b > 0) {
            map.put("success", Boolean.TRUE);
            map.put("msg", "删除成功!");
        } else {
            map.put("success", Boolean.FALSE);
            map.put("msg", "删除失败，请重试!");
        }
        return map;
    }
}
